package Server.service.impl;

import Plug.result.RetResponse;
import Plug.result.RetResult;
import Server.dao.*;
import Server.entity.*;
import Server.service.OrderRecordService;
import Server.service.OrderService;
import org.apache.shiro.SecurityUtils;
import org.aspectj.apache.bcel.generic.RET;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private UsersSaleDao usersSaleDao;
    @Autowired
    private OrderRecordService orderRecordService;
    @Autowired
    private ShopOrderDao shopOrderDao;
    @Autowired
    private DictionarysDao dictionarysDao;
    @Autowired
    private ShopCarDao shopCarDao;
    @Autowired
    private ShopSpecDao shopSpecDao;
    @Autowired
    private ShopOrderItemDao shopOrderItemDao;
    @Autowired
    private ShopOrderSaleDao shopOrderSaleDao;
    @Autowired
    private UsersDao usersDao;
    @Autowired
    private CityShopDao cityShopDao;
    @Autowired
    private UsersCityDao usersCityDao;
    @Autowired
    private InviteUsersDao inviteUsersDao;
    @Autowired
    private CityDao cityDao;
    @Autowired
    private MessageDao messageDao;
    @Transactional
    @Override
    public RetResult addOrder(List<ShopCar> shopCar, List<UsersSale> usersSales,UserSite userSite) {
        if(userSite==null){
            return RetResponse.makeErrRsp("请选择收货地址");
        }
        BigDecimal cityMoney = BigDecimal.ZERO;
        BigDecimal payMoney=BigDecimal.ZERO;
        for(ShopCar cr:shopCar){
            if(!userSite.getUsers().equals(cr.getUsers())){
                return RetResponse.makeErrRsp("请选择自己的收货地址");
            }
            /*
             * 数量是否充足
             *
             * */
            if(cr.getCount()>cr.getShopSpec().getRepertory()){
                return RetResponse.makeErrRsp("库存不不足");
            }
            /*
             *
             * 优惠券所有金额并作废优惠券
             *
             * */
            if(cr.getCount()<cr.getShopSpec().getBuyCount()){
                return RetResponse.makeErrRsp("您的购买数量未达到起订数量");
            }
            /*
             *
             * 应支付金额
             *
             * */
            Users fspuser = (Users) SecurityUtils.getSubject().getSession().getAttribute("Users");
            fspuser = usersDao.findByUuid(fspuser.getUuid());

            BigDecimal bp=BigDecimal.ZERO;
            if(fspuser.getType().equals(Users.Type.USER)){
                CityShop cityShop = cityShopDao.findByShopSpecAndCity(cr.getShopSpec(),cityDao.findByCityLike(fspuser.getAddress()));
                if(cityShop!=null){
                    bp =cityShop.getMoney().multiply(new BigDecimal(cr.getCount()));
                    cityMoney.add((cityShop.getMoney().subtract(cr.getShopSpec().getPrice())).multiply(new BigDecimal(cr.getCount())));
                }else{
                    bp = cr.getShopSpec().getPrice().multiply(new BigDecimal(cr.getCount()));
                }
            }else{
                bp = cr.getShopSpec().getPrice().multiply(new BigDecimal(cr.getCount()));
            }
            payMoney=payMoney.add(bp);
        }

        BigDecimal saleMoney = new BigDecimal(0);
        for(UsersSale usersSale:usersSales){
            if(usersSale.getSale().getStatus().equals(Sale.Status.NO)){
                return RetResponse.makeErrRsp("优惠券【"+usersSale.getSale().getName()+"】已过期！");
            }else if(usersSale.getStatus().equals(UsersSale.Status.NO)){
                return RetResponse.makeErrRsp("优惠券【"+usersSale.getSale().getName()+"】已使用！");
            }
            for(ShopCar cr:shopCar){
                if(!usersSale.getUsers().equals(cr.getUsers())){
                    return RetResponse.makeErrRsp("请选择自己的优惠券");
                }
            }
            saleMoney=saleMoney.add(usersSale.getSale().getMoney());
            usersSale.setStatus(UsersSale.Status.NO);
            usersSaleDao.save(usersSale);
        }
        ShopOrder shopOrder = new ShopOrder();
        shopOrder.setSaleMoney(saleMoney);
        shopOrder.setIncomeMoney(payMoney);
        payMoney=payMoney.subtract(saleMoney).compareTo(BigDecimal.ZERO)<0?BigDecimal.ZERO:payMoney.subtract(saleMoney);
        shopOrder.setMoney(payMoney);
        shopOrder.setUsers(userSite.getUsers());
        shopOrder.setStatus(ShopOrder.Status.NO);
        shopOrder.setUserSite(userSite);
        shopOrder.setCityMoney(cityMoney);
        shopOrder=shopOrderDao.save(shopOrder);
        for(UsersSale usersSale:usersSales){
            ShopOrderSale shopOrderSale  = new ShopOrderSale();
            shopOrderSale.setShopOrder(shopOrder);
            shopOrderSale.setUsersSale(usersSale);
            shopOrderSaleDao.save(shopOrderSale);
        }




        /*
        *
        * 订单项
        *
        * */

        /*
        *
        * 总数量
        *
        * */
        Integer count = 0;
        for(ShopCar cr:shopCar){
            ShopOrderItem shopOrderItem  = new ShopOrderItem();
            shopOrderItem.setCount(cr.getCount());
            count=count+cr.getCount();
            shopOrderItem.setMoney(cr.getShopSpec().getPrice().multiply(new BigDecimal(cr.getCount())));
            shopOrderItem.setShopSpec(cr.getShopSpec());
            shopOrderItem.setShopOrder(shopOrder);
            shopOrderItemDao.save(shopOrderItem);
            ShopSpec spec = cr.getShopSpec();
            spec.setRepertory(spec.getRepertory()-cr.getCount());
            shopSpecDao.save(spec);
            for(UsersSale usersSale:usersSales){
                orderRecordService.del(usersSale.getSale().getMoney(), OrderRecord.Type.SALE,usersSale.getUuid(),shopOrder.getUuid());
            }
            shopCarDao.deleteByUuid(cr.getUuid());
        }
        shopOrder.setCount(count);
        shopOrder=shopOrderDao.save(shopOrder);
        /*
        *
        * 消息提示
        *
        * */
        Message msg = new Message();
        msg.setUser(usersDao.findByUuid("1"));
        msg.setReadMI(0);
        msg.setMessage("您有新订单啦~ 点击a(https://kainuo.vip:449/b2b2c/vsp/71?uuid="+shopOrder.getUuid()+")[https://kainuo.vip:449/b2b2c/vsp/71?uuid="+shopOrder.getUuid()+"]");
        msg.setShopOrder(shopOrder);
        messageDao.save(msg);
        return RetResponse.makeOKRsp(shopOrder);
    }
}